import Utils.HttpUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author: Enid
 * @date: 2025/10/9 16:14
 * @description: 物流信息查询API
 */
public class LogisticsApi {
    // 物流信息字段中文映射表
    private static final Map<String, String> FIELD_NAME_MAP = new HashMap<String, String>() {{
        put("cpCode", "快递公司编码");
        put("mailNo", "快递单号");
        put("theLastTime", "最后更新时间");
        put("cpMobile", "快递公司电话");
        put("theLastMessage", "最后状态信息");
        put("orderNo", "订单号");
        put("logisticsCompanyName", "快递公司名称");
        put("cpUrl", "公司网址");
        put("logisticsStatusDesc", "物流状态描述");
        put("logisticsTraceDetailList", "物流跟踪详情");
        put("logisticsStatus", "物流状态码");
    }};


    public static void main(String[] args) {
        String host = "https://kzexpress.market.alicloudapi.com";
        String path = "/api-mall/api/express/query";
        String method = "GET";
        String appcode = "380164e0b882475ead478d7ccc3c77ef";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("expressNo", "9810331886691");
        querys.put("mobile", "4019");

        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            // 解析JSON响应
            ObjectMapper mapper = new ObjectMapper();
            JsonNode rootNode = mapper.readTree(EntityUtils.toString(response.getEntity()));

            // 提取物流信息数据
            JsonNode dataNode = rootNode.get("data");
            if (dataNode != null) {
                System.out.println("物流信息：");
                displayLogisticsInfo(dataNode);
            } else {
                System.out.println("未找到物流信息数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示物流信息详情
     * @param logisticsNode 物流信息JSON节点
     */
    private static void displayLogisticsInfo(JsonNode logisticsNode) {
        Iterator<Map.Entry<String, JsonNode>> fields = logisticsNode.fields();
        while (fields.hasNext()) {
            Map.Entry<String, JsonNode> field = fields.next();
            String chineseName = FIELD_NAME_MAP.getOrDefault(field.getKey(), field.getKey());
            System.out.println("  " + chineseName + ": " + field.getValue().asText());
        }
    }
}
